<?php

namespace App\Kuafu\Mybank\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Shop\Model\Shop;
use App\Library\SnowFlake\SnowFlake;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;

class MybankOrderRecordModel extends BaseModel
{
    use HasFactory, KfSoftDeletes, KfUid;

    protected $connection = 'mysql';
    protected $table      = "mybank_order_record";

    protected $primaryKey   = 'orderId';
    public    $incrementing = false;

    protected $fillable = [
        'orderId',
        'tranId',
        'shopId',
        'merchantId',
        'title',
        'remark',
        'brokerage',
        'paidBrokerage',
        'refundAmount',
        'netPaidAmount',
        'refundStatus',
        'type',
        'completeAt',
        'createdAt',
        'updatedAt',
        'createdByUid',
        'updatedByUid',
        'isDeleted',
    ];

    const TYPE_DEFAULT       = 0; // 默认类型
    const TYPE_BROKERAGE     = 1; // 佣金
    const TYPE_KFOS_ORDER    = 2; // 订货
    const TYPE_BILL_TYPE_ELE = 3; // 饿了么欠款, 直接批次里扣除
    const TYPE_BILL_TYPE_MT  = 4; // 美团欠款, 直接批次里扣除


    /**
     * 分账 类型汇总
     */
    const TYPE_SEPARATE_BILL_TYPE = [
        self::TYPE_BROKERAGE,
        self::TYPE_BILL_TYPE_ELE,
        self::TYPE_BILL_TYPE_MT,
    ];

    /**
     * 账单类型
     */
    const TYPE_TEXT = [
        self::TYPE_DEFAULT       => '默认',
        self::TYPE_BROKERAGE     => '佣金',
        self::TYPE_KFOS_ORDER    => '订货',
        self::TYPE_BILL_TYPE_ELE => '饿了么欠款',
        self::TYPE_BILL_TYPE_MT  => '美团欠款',
    ];


    /**
     * 钱包展示类型
     */
    const WALLET_TYPE_SHOW_TYPE_TEXT = [
        self::TYPE_DEFAULT    => '默认',
        self::TYPE_BROKERAGE  => '服务费',
        self::TYPE_KFOS_ORDER => '订货支出',
    ];


    const PAY_STATUS_DEFAULT = 0;
    const PAY_STATUS_PAYING  = 1;
    const PAY_STATUS_PAID    = 2;

    const PAY_STATUS_TEXT = [
        self::PAY_STATUS_DEFAULT => '未支付',
        self::PAY_STATUS_PAYING  => '部分支付',
        self::PAY_STATUS_PAID    => '已支付',
    ];

    const REFUND_STATUS_DEFAULT   = 0;
    const REFUND_STATUS_REFUNDING = 1;
    const REFUND_STATUS_REFUNDED  = 2;


    const REFUND_STATUS_TEXT = [
        self::REFUND_STATUS_DEFAULT   => '未退款',
        self::REFUND_STATUS_REFUNDING => '退款中',
        self::REFUND_STATUS_REFUNDED  => '已退款',
    ];


    public function orderRecordWithhold()
    {

    }

    /**
     * 关联数据
     *
     * @return HasManyThrough
     */
    public function orderWithholdRecordHasManyThrough(): HasManyThrough
    {
        return $this->hasManyThrough(
            MybankOrderWithholdRecordModel::class,
            MybankOrderRecordWithholdMappingsModel::class,
            'orderRecordId',
            'id',
            'orderId',
            'orderWithholdRecordId'
        );
    }


    /**
     * 更新账单退款状态
     *
     * @param string $tranId
     * @return Builder|Model|object|void
     */
    public function updateBillOrderRefundStatus(string $tranId)
    {
        $orderRecord = self::query()->where('tranId', $tranId)->first();

        if (empty($orderRecord)) {
            return;
        }

        // TODO 判断是否已经退款中

        $orderRecord->refundStatus = MybankOrderRecordModel::REFUND_STATUS_REFUNDING;
        $orderRecord->save();

        return $orderRecord;
    }

    public function getOrderWithholdRecordByType($type)
    {
        // 获取所有的支付账单记录
        return $this->orderWithholdRecordHasManyThrough()->where('tranType', $type)->get();
    }

    /**
     * 生成账单支付账单
     *
     * @param string $shopId
     * @param string $merchantId
     * @param string $remark
     * @param string $brokerage
     * @param string $tranId
     * @param string $title
     * @param int $type
     * @return MybankOrderRecordModel|Builder|Model|object
     */
    public function genBillOrder(
        string $shopId,
        string $merchantId,
        string $remark,
        string $brokerage,
        string $tranId,
        string $title = '技术服务费',
        int $type = MybankOrderRecordModel::TYPE_BROKERAGE
    ) {

        $orderRecord = self::query()->where('tranId', $tranId)->first();

        if ($orderRecord) {
            return $orderRecord;
        }
        $orderRecord                = new MybankOrderRecordModel();
        $orderRecord->orderId       = SnowFlake::nextIdCache();
        $orderRecord->shopId        = $shopId;
        $orderRecord->merchantId    = $merchantId;
        $orderRecord->title         = $title;
        $orderRecord->remark        = $remark;
        $orderRecord->tranId        = $tranId;
        $orderRecord->brokerage     = $brokerage;
        $orderRecord->paidBrokerage = 0;
        $orderRecord->type          = $type;
        $orderRecord->payStatus     = MybankOrderRecordModel::PAY_STATUS_DEFAULT;
        $orderRecord->save();

        return $orderRecord;
    }

}
